<?php
namespace app\common\service;

use app\common\logic\UsersLogic;
use app\common\logic\wechat\WechatUtil;
use think\Controller;
use think\Model;
use think\Request;
use think\Verify;
use think\facade\Db;
use basic\ApiBase;
class Base extends ApiBase {
    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->is_weixin=is_weixin();
        $this->weixin_config = Db::name('wx_set')->find();
        //推广
        if(input('tjr_id')) {
            session('sys_tjr_id',input('tjr_id'));
        }
        if(input('goods_id')) {
            session('goods_id',input('goods_id'));
        }

        if(input('reg_id')){
            session('reg_id',input('reg_id'));
        }
        //后台登录
//        if(session('admin_id','','syffff') && ACTION_NAME=='hylogins') {
//            session('user_id',null);
//        }
        $sys_close = tpCache('basic.sys_close');
        if($sys_close){
            $close_reason = tpCache('basic.sys_close_memo');
            $result = [
                'status'=> 0,
                'msg' => '网站已关闭:'.$close_reason,
            ];
        }

        $token = input('token');
        $user = json_decode(base64_decode($token),true);
        $ip = request()->ip();
        if($user && $user['ip']!=$ip && self::$AUTH_CHECK==true){
            $result = [
                'status'=>false,
                'msg' => '检测到您的IP发生变化，请重新登录',
                'data' =>[
                    'need_login' => 1
                ]
            ];
        }
        if(!empty($user['user_id'])){
            $this->user_id = $user['user_id'];
        }
        if(!empty($user['sid'])){
            $this->sid = $user['sid'];
        }

//        $user = [
//            'user_id' => 1,
//            'login_time' => time(),
//            'ip' => '127.0.0.1'
//        ];
        $controller = request()->controller();
        $action = request()->action();
        $plate = platform();
        //会员
        if($controller!='WxAppLogin'  && !in_array($action,['loginset','reg'])){
            if (!empty($user['user_id'])) {
                $this->user_id = $user['user_id'];

                $user_info = logic('UsersLogic')->get_info($this->user_id);
                if(!$user_info || $user_info['is_lock']==1) {
                    $result = [
                        'status'=>false,
                        'msg' => '用户已锁定',
                        'data' =>[
                            'need_login' => 1
                        ]
                    ];
                   //return $result;
                }
                $login_time = $user['login_time'];
                $dif_time = time()-$login_time;
                if($dif_time>=86400){
                    $result = [
                        'status'=>false,
                        'msg' => '请登录',
                        'data' =>[
                            'need_login' => 1
                        ]
                    ];
                    //return $result;
                }
                $this->user = $user_info;

                session('user',$this->user);
            }elseif(static::$AUTH_CHECK){
                $mast_login = tpCache('reg.login_mast_'.$plate);
                if($mast_login==1){
                    //强制登录
                    $result = [
                        'status'=>false,
                        'msg' => '请重新登录',
                        'data' =>[
                            'need_login' => tpCache('reg.login_mast_'.$plate)
                        ]
                    ];
                }elseif($mast_login==0 && strtolower(input('service').input('method'))!='userinfo'){
                    //不强制登录
                    $result = [
                        'status'=>false,
                        'msg' => '请重新登录',
                        'data' =>[
                            'need_login' => 1
                        ]
                    ];
                }
                //exit(json_encode($result));
                //return $result;exit;
            }
        }
        if(!empty($result) && !$result['status']){
            $this->apilog(false,$result);
            exit(json_encode($result));
        }


        //钱包
        $this->wallet = logic('Wallet')->getName();
        $this->wallet['onlinepay'] = '在线支付';

        if($plate=='wx'){
            $wx_set = Db::name('wx_set')->where('1=1')->find();
        }else if($plate=='wx_app'){
            $wx_set = Db::name('wx_app_set')->where('1=1')->find();
        }

        $shop_set = [
            'comment' => tpCache('shop.comment'),
            'appid' => $wx_set['appid']??0,
            'appsecret' => $wx_set['appsecret']??0,
            'login_setnickname' => tpCache('reg.login_setnickname'),
            'login_bind' => tpCache('reg.login_bind_mobile'),
            'can_cart' => tpCache('shop.can_cart'),
            'can_buy' => tpCache('shop.can_buy'),
        ];

        self::$shop_set = $shop_set;

    }
    public function apilog($status,$result){
        //数据
        $args = input(false); //不过滤，防新guid被过滤
        //移除公共的路由参数
        unset($args['service'],$args['method'],$args['s']);
        $arg = $args;


        //请求数据
        $data_log = [
            "api" => input('service').'/'.input('method'),
            'arg' => json_encode($arg, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES + JSON_NUMERIC_CHECK),
            "ip" => request()->ip(),
            "w_time" => time(),
            'headers' => json_encode(request()->header(), JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES + JSON_NUMERIC_CHECK),
        ];
        //当前用户Id

        if(!empty($args['token'])) {
            $user = json_decode(base64_decode($args['token']),true);
            $data_log['user_id'] = $user['user_id']??'';
        }
        //返回结果
        $data_log['status'] = $status;

        $data_log['result'] = json_encode($result, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES + JSON_NUMERIC_CHECK);
        if(strlen($data_log['result']) > 60000) {
            $data_log['result'] = '数据太多，不记录';
        }

        //保存
        $request_id = Db::name('api_log')->insertGetId($data_log);
    }



}
